package com.rlz.framemm;

import com.rlz.ams.hook.Hook;
import com.rlz.aspectj.AppInterceptor;
import com.rlz.base.app.Base;
import com.rlz.base.cache.CacheManager;
import com.rlz.base.cache.User;
import com.rlz.base.util.ThreadUtil;
import com.rlz.base.util.ToastUtils;
import com.rlz.framemm.config.ImageLoaderStrategy;
import com.rlz.framemm.util.ActivityManager;
import com.rlz.framemm.util.NightManager;
import com.rlz.image.imageloader.ImageLoader;

/**
 * Created by RLZ
 * on 2021/4/8
 */
public class App extends Base {

    private final ActivityManager mManager = new ActivityManager();
    public static App INSTANCE;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        ImageLoader.get().set(new ImageLoaderStrategy());

        AppInterceptor.setInterceptor((type, joinPoint) -> {
            if (type == 222) {
                if (CacheManager.Companion.getManager().getUserInfo() == null) {
                    ToastUtils.showToast("没有登录");
                    User user = new User();
                    user.token = "123";
                    CacheManager.Companion.getManager().setUserInfo(user);
                    return true;
                }
            }
            return false;
        });

       /* new Handler(Looper.getMainLooper()).post(() -> {
            while (true) {
                try {
                    Looper.loop();
                } catch (Exception e) {


                    P.e("捕获异常主线程:",
                            Thread.currentThread().getName() +
                                    "在:" + " @(" + e.getStackTrace()[0].getClassName() + ")" +
                                    "第:" + e.getStackTrace()[0].getLineNumber() + "行");
                    e.printStackTrace();
                }
            }
        });*/

        ThreadUtil.runOnUiThread(() -> Hook.hookAms(this), 500);
        registerActivityLifecycleCallbacks(mManager);
        NightManager.Companion.get().initNightMode();
    }

    public ActivityManager getManager() {
        return mManager;
    }
}
